import { Component, ChangeDetectionStrategy, OnInit, ChangeDetectorRef } from '@angular/core';
import { _HttpClient } from '@delon/theme';

@Component({
  selector: 'notifications-subscribe',
  templateUrl: './subscribe.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NotificationsSubscribeComponent implements OnInit {
  data = [];
  constructor(private http: _HttpClient, private cdr: ChangeDetectorRef) {}
  loading = false;

  ngOnInit() {
    this.loadSysType();
  }

  loadSysType() {
    this.loading = true;
    this.http.get(`/cf/admin/web-msg/subscribe`).subscribe(res => {
      res.data.forEach(r => {
        r.msg = !r.msg || r.msg === '0' ? false : true;
        r.sms = !r.sms || r.sms === '0' ? false : true;
        r.ding2 = !r.ding2 || r.ding2 === '0' ? false : true;
        r.email = !r.email || r.email === '0' ? false : true;
      });
      this.data = res.data;

      this.loading = false;
      this.cdr.detectChanges();
    });
  }

  setRefuse(p, val) {
    const param = { ...p };
    param.msg = p.msg ? '1' : '0';
    param.ding2 = p.ding2 ? '1' : '0';
    param.sms = p.sms ? '1' : '0';
    param.email = p.email ? '1' : '0';
    this.http.post(`/cf/admin/web-msg/subscribe`, param).subscribe(res => {});
  }
}
